package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.ChannelMapper;
import com.heima.admin.service.ChannelService;
import com.heima.common.exception.CustException;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, AdChannel> implements ChannelService {
    /**
     * 分页查询结果
     * 根据：频道名称 频道状态 分页数据 分页查询频道列表
     * ChannelDTO
     */
    @Override
    public PageResponseResult findByQuery(ChannelDTO dto) {
        //1.参数校验代码
        dto.checkParam();
        //2.分页查询
        //2.1封装分页查询条件
        Page<AdChannel> pageReq = new Page<>(dto.getPage(), dto.getSize());
        //2.2其它查询条件
        LambdaQueryWrapper<AdChannel> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNoneBlank(dto.getName())) {//true代表name不为空
            queryWrapper.like(AdChannel::getName, dto.getName());
        }
        if (dto.getStatus() != null) {//如果状态不为null，使用状态查询
            queryWrapper.eq(AdChannel::getName, dto.getName());
        }
        //2.3按照ord升序排序
        queryWrapper.orderByAsc(AdChannel::getOrd);
        //2.4执行查询操作
        //参数1：分页条件   参数2：查询条件
        IPage<AdChannel> pageResult = this.page(pageReq, queryWrapper);
        //3.封装成PageResponseResult结果返回
        return new PageResponseResult(dto.getPage(), dto.getSize(), pageResult.getTotal(), pageResult.getRecords());
    }

    @Override
    public ResponseResult insert(AdChannel adChannel) {
        //1.校验参数
        //1.1名字不能为空
        String name = adChannel.getName();
        if (StringUtils.isNoneBlank(name)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道名称不能为空");
        }
        //1.2名称长度不能大于10
        if (name.length() > 10) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道名称长度不能大于10");
        }
        //1.3校验名称不允许重复
        int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, name));
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "频道已经存在");
        }
        //2.将频道保存到数据库
        adChannel.setCreatedTime(new Date());
        this.save(adChannel);
        //3.返回成功的响应
        return ResponseResult.okResult();
    }

    @Transactional
    @Override
    public ResponseResult update(AdChannel adChannel) {
        //1.参数校验
        //1.1检查频道id
        if (adChannel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道id不能为空");
        }
        //1.2根据id查询频道信息
        AdChannel oldChannel = getById(adChannel.getId());
        if (oldChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "频道不存在");
        }
        //1.3判断参数name是否为空，如果不为空，判断是否和原来频道是否一致
        String name = adChannel.getName();
        //频道名称不为空，并且频道名称做了改变，需要校验一下频道名称
        if (StringUtils.isNoneBlank(name) && !name.equals(oldChannel.getName())) {
            //名称长度不能大于10
            if (name.length() > 10) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道名称长度不能大于10");
            }
            //校验名称不允许重复
            int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, name));
            if (count > 0) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "频道已经存在");
            }
        }
        //2.修改频道
        updateById(adChannel);
        //3.响应结果
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteById(Integer id) {

        if (id == 1) {
            System.out.println(1 / 0);
        }
        //1.检查参数
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.判断当前频道是否存在 和 是否有效
        AdChannel adChannel = getById(id);
        if (adChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 启用状态下不能删除
        if (adChannel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //3.删除频道
        removeById(id);
        if (id==50){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW);
        }
        //4.返回响应结果
        return ResponseResult.okResult();
    }
}
